package com.MP_fianl;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Account extends ActiveRecord {
	 private static final String TAG = "CameraTest";
	public static final String COL_ROW_ID = "_id";
	public static final String COL_USERNAME = "username";
	public static final String COL_DATE = "date";
	public static final String COL_PREFERENCE = "preference";
	public static final String COL_DESCRIPTION = "description";
	public static final String COL_IMAGEID = "image";

	public static final String SQL_TABLE_NAME = "closet_db";
	public static final String SQL_CREATE_TABLE = "CREATE TABLE "
			+ SQL_TABLE_NAME + " "
			+ " (_id integer primary key autoincrement, "
			+ "username text not null, "
			+ "date text not null, "
			+ "preference integer not null, "
			+ "image integer not null, "
			+ "description text not null); ";

	private SQLiteDatabase sqliteDatabase;

	public int getId() {
		return _id;
	}
	public void setId(int id) {
		this._id = id;
	}
	public String getUsername() {
		return _username;
	}
	public void setUsername(String username) {
		this._username = username;
	}
	public String getDate(){
		return _date;
	}
	public void setDate(String date){
		this._date = date;
	}
	public int getPreference(){
		return _preference;
	}
	public void setPreference(int pref){
		this._preference = pref;
	}
	public String getDescription() {
		return _description;
	}
	public void setDescription(String str){
		this._description = str;
	}
	public int getImageID(){
		return _imageid;
	}
	public void setImageID(int imageid){
		this._imageid = imageid;
	}
	private int _id;
	private String _date;
	private String _username;
	private int _preference;
	private String _description;
	private int _imageid;

	@Override
	public boolean loadbyID(Activity activity) {
		Cursor cursor = sqliteDatabase.query(true, SQL_TABLE_NAME,
				new String[] { COL_ROW_ID, COL_DATE, COL_USERNAME,
						COL_PREFERENCE,COL_IMAGEID, COL_DESCRIPTION }, COL_ROW_ID + "=" + _id, null, null,
				null, null, null);
		return search(cursor, activity);
	}
	
	private boolean search(Cursor cursor, Activity activity){
		if (cursor != null) {
			cursor.moveToFirst();
			if(cursor.getCount() < 1 ){
				return false;
			}
			activity.startManagingCursor(cursor);	
			setId(cursor.getInt(cursor.getColumnIndex(COL_ROW_ID)));
			setUsername(cursor.getString(cursor.getColumnIndex(COL_USERNAME)));
			setDate(cursor.getString(cursor.getColumnIndex(COL_DATE)));
			setPreference(cursor.getInt(cursor.getColumnIndex(COL_PREFERENCE)));
			setDescription(cursor.getString(cursor.getColumnIndex(COL_DESCRIPTION)));
			setImageID(cursor.getInt(cursor.getColumnIndex(COL_IMAGEID)));	
		}
		return true;
	}
   public boolean loadbyImageID(Activity activity){
		Cursor cursor = sqliteDatabase.query(true, SQL_TABLE_NAME,
				new String[] { COL_ROW_ID, COL_DATE, COL_USERNAME,
						COL_PREFERENCE,COL_IMAGEID,COL_DESCRIPTION}, COL_IMAGEID + "=" + _imageid, null, null,
				null, null, null);
		return search(cursor, activity);
   }
	
	@Override
	public Cursor retrieveAll() {
		return sqliteDatabase.query(SQL_TABLE_NAME, new String[] { COL_ROW_ID,
				COL_USERNAME, COL_DATE, COL_PREFERENCE, COL_IMAGEID,COL_DESCRIPTION}, null, null,
				null, null, null);
	}

	@Override
	public long save() {
		ContentValues values = new ContentValues();
		if (_id <= 0) {
			values.put(COL_USERNAME, _username);
			values.put(COL_DATE, _date);
			values.put(COL_PREFERENCE, _preference);
			values.put(COL_DESCRIPTION, _description);
			values.put(COL_IMAGEID, _imageid);
			return sqliteDatabase.insert(SQL_TABLE_NAME, null, values);
		} else {
			values.put(COL_USERNAME, _username);
			values.put(COL_DATE, _date);
			values.put(COL_PREFERENCE, _preference);
			values.put(COL_DESCRIPTION, _description);
			values.put(COL_IMAGEID, _imageid);
			return sqliteDatabase.update(SQL_TABLE_NAME, values, COL_ROW_ID
					+ "=" + _id, null);
		}
	}

	public boolean delete() {
		return sqliteDatabase.delete(SQL_TABLE_NAME, COL_ROW_ID + "=" + _id,
				null) > 0;
	}
	

	@Override
	public void setSQLiteDatabase(SQLiteDatabase sqliteDatabase) {
		this.sqliteDatabase = sqliteDatabase;
	}
}





